<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="../../assets/img/login_icon.png" alt="" />
        <span class="title">黑马面面</span><span class="line"></span>
        <span class="sub-title">用户登录</span> </div>
       <!-- 使用element的from表单验证组件 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="login-Form"
        >
          <el-form-item prop="phone">
            <el-input v-model="ruleForm.phone" prefix-icon="el-icon-user" placeholder="请输入手机号"></el-input>
          </el-form-item>
           <el-form-item prop="password">
            <el-input type="password" v-model="ruleForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-row>
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                v-model="ruleForm.code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col style="margin-left:10px;" :span="7">
            <img
              @click="changeLoginCaptcha"
               style="width:100%;height:40px;"
              :src="loginCaptcha"
              alt=""
            />
          </el-col>
        </el-row>
         <el-form-item prop="checked">
        <el-checkbox v-model="ruleForm.checked"></el-checkbox> 我已阅读并同意<el-link href="https://www.baidu.com/" type="primary" target="__blank" hr>用户协议</el-link>和 <el-link type="primary" target="__blank" href="https://www.baidu.com/">隐私条款</el-link>
        </el-form-item>
         <el-form-item>
           <el-button type="primary" style="width:100%;" @click="login()">登陆</el-button>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width:100%;" @click='register'>注册</el-button>
          </el-form-item>
        </el-form>
    </div>
    <div class="right">
      <img src="../../assets/img/login_bg.png" alt="" />
    </div>
    <!-- 子组件当标签一样使用 -->
    <register ref="register"></register>
  </div>
</template>
<script>
// 按需导出,@可以直接进入src文件夹
import { saveToken } from '@/utils/token'
// 导入子组件
import register from './register'

export default {
  name: 'login',
  // 注册子组件
  components: {
    register
  },
  data () {
    return {
      // 请求验证码由图片src发出,全局配置axios
      loginCaptcha: `${process.env.VUE_APP_BASEURL}/captcha?type=login`,
      ruleForm: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: false
      },
      rules: {
        phone: [{
          // rule规则
          // value输入的值
          // callback回调函数
          // validator是校验规则
          validator: (rule, value, callback) => {
            if (!value) return callback(new Error('手机号不得为空'))
            // 正则判断手机号格式
            const reg = /^1[3456789][0-9]{9}$/
            if (!reg.test(value)) return callback(new Error('手机号不合法'))
            // 来到这就是手机号合法
            callback()
          },
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        },
        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        code: [{
          required: true,
          message: '验证码不能为空',
          trigger: 'blur'
        }],
        checked: [{
          validator: (rule, value, callback) => {
            value ? callback() : callback(new Error('请阅读协议并选中'))
          },
          trigger: 'change'
        }]
      }
    }
  },
  methods: {
    // 点击的时候利用时间戳来刷新
    changeLoginCaptcha () {
      this.loginCaptcha = `${process.env.VUE_APP_BASEURL}/captcha?type=login&uu=${new Date() - 0}`
    },
    login () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return

        /** 使用promise方式 */
        /**
        this.$axios.post('/login', this.ruleForm).then(res => {
          if (res.data.code === 200) {
            this.$message.success('登录成功~')

            // 保存token到本地
            localStorage.setItem('heimamm64-token', res.data.data.token)

            // 跳转到layout页面
            this.$router.push('/layout')
          } else {
            // 没成功
            this.$message.error(res.data.message)

            // 刷新验证码
            this.changeLoginCaptcha()
          }
        })
         */
        // await 后面接的就是promise对象
        const res = await this.$axios.post('/login', this.ruleForm)

        if (res.code === 200) {
          this.$message.success('登录成功~')

          // 保存token到本地
          // localStorage.setItem('heimamm64-token', res.data.token)
          // 记得传实参给形参token
          saveToken(res.data.token)

          // 跳转到layout页面
          this.$router.push('/layout')
        } else {
          // 没成功
          this.$message.error(res.message)

          // 刷新验证码
          this.changeLoginCaptcha()
        }
      })
    },
    register () {
      this.$refs.register.dialogVisible = true
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  //   右边盒子
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    //   头部盒子
    .title-box {
      display: flex;
      align-items: center;
      //   图片
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      // 黑马面面
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      // 分格线
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      // 用户登录
      .sub-title {
        font-size: 22px;
      }
    }
  }
  .login-Form{
    margin-top: 29px;
    display: block;
}
}
</style>
